package com.gitee.wsl.android.ext

import android.annotation.SuppressLint
import android.content.res.Resources
import android.content.res.TypedArray
import android.graphics.BitmapFactory
import android.graphics.Color
import android.graphics.Paint
import android.graphics.drawable.Drawable
import android.view.View
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import com.gitee.wsl.android.ext.ApplicationInit.application
import com.gitee.wsl.android.ext.math.pxToDp
import kotlin.math.ceil

object ResouresExt{

    @ColorInt
    fun parseColor(colorString: String): Int =
        Color.parseColor(colorString)


    fun calculateTextHeight(textSizePx:Float):Int{
        val p = Paint()
        p.textSize = textSizePx
        val fm: Paint.FontMetrics = p.fontMetrics
        return ceil(fm.bottom - fm.top).toInt()
    }

    /**
     * Return the drawable identifier by name.
     *
     * @param name The name of drawable.
     * @return the drawable identifier by name
     */
    @SuppressLint("DiscouragedApi")
    fun getDrawableIdByName(name: String): Int {
        return application.resources
            .getIdentifier(name, "drawable", application.packageName)
    }

    /**
     * Return the mipmap identifier by name.
     *
     * @param name The name of mipmap.
     * @return the mipmap identifier by name
     */
    @SuppressLint("DiscouragedApi")
    fun getMipmapIdByName(name: String): Int {
        return application.resources
            .getIdentifier(name, "mipmap", application.packageName)
    }

    /**
     * Return the layout identifier by name.
     *
     * @param name The name of layout.
     * @return the layout identifier by name
     */
    @SuppressLint("DiscouragedApi")
    fun getLayoutIdByName(name: String): Int {
        return application.resources
            .getIdentifier(name, "layout", application.packageName)
    }

    /**
     * Return the style identifier by name.
     *
     * @param name The name of style.
     * @return the style identifier by name
     */
    @SuppressLint("DiscouragedApi")
    fun getStyleIdByName(name: String): Int {
        return application.resources
            .getIdentifier(name, "style", application.packageName)
    }

    /**
     * Return the anim identifier by name.
     *
     * @param name The name of anim.
     * @return the anim identifier by name
     */
    @SuppressLint("DiscouragedApi")
    fun getAnimIdByName(name: String): Int {
        return application.resources
            .getIdentifier(name, "anim", application.packageName)
    }

    /**
     * Return the menu identifier by name.
     *
     * @param name The name of menu.
     * @return the menu identifier by name
     */
    @SuppressLint("DiscouragedApi")
    fun getMenuIdByName(name: String): Int {
        return application.resources
            .getIdentifier(name, "menu", application.packageName)
    }
}

fun Resources.isRtl():Boolean=this.configuration.layoutDirection == View.LAYOUT_DIRECTION_RTL

@SuppressLint("DiscouragedApi")
fun Resources.getDimenByName(resName: String, defaultValue: Int): Int {
    val frameSize: Int
    val frameSizeResID = this.getIdentifier(resName, "dimen", "android")
    if (frameSizeResID != 0) {
        frameSize = this.getDimensionPixelSize(frameSizeResID)
    } else {
        frameSize = defaultValue.pxToDp()
    }
    return frameSize
}

@SuppressLint("DiscouragedApi")
fun Resources.getBoolByName(resName: String, defaultValue: Boolean): Boolean {
    val `val`: Boolean
    val resId = this.getIdentifier(resName, "bool", "android")
    `val` = if (resId != 0) {
        this.getBoolean(resId)
    } else {
        defaultValue
    }
    return `val`
}

@SuppressLint("DiscouragedApi")
fun Resources.getIntegerByName(resName: String, defaultValue: Int): Int {
    val resId = this.getIdentifier(resName, "integer", "android")
    return if (resId != 0) this.getInteger(resId) else defaultValue
}

fun Resources.getDrawableById(@DrawableRes id:Int) = ContextCompat.getDrawable(application, id)

fun Resources.getBitmap(@DrawableRes id:Int) = BitmapFactory.decodeResource(this,id)

fun Resources.getIntArrayOrNull(id: Int): IntArray? {
    return try {
        getIntArray(id)
    } catch (e: Resources.NotFoundException) {
        null
    }
}

@SuppressLint("UseCompatLoadingForDrawables")
fun Resources.getDrawableOrNull(id: Int, theme: Resources.Theme? = null): Drawable? {
    return try {
        getDrawable(id, theme)
    } catch (e: Resources.NotFoundException) {
        null
    }
}

fun Resources.obtainTypedArrayOrNull(id: Int): TypedArray? {
    return try {
        obtainTypedArray(id)
    } catch (e: Resources.NotFoundException) {
        null
    }
}